package ${basePackage}.${module}.service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import java.time.LocalDateTime;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import ${basePackage}.common.pojo.*;
import ${basePackage}.${module}.mapper.${slave.className}Mapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.apache.commons.lang3.StringUtils;

import ${basePackage}.${module}.domain.*;
import com.baomidou.mybatisplus.plugins.Page;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

@Service
@CacheConfig(cacheNames = "${slave.camelName}")
public class ${slave.className}Service extends ServiceImpl<${slave.className}Mapper, ${slave.className}DO> {

    @Cacheable
    public Page<${slave.className}DO> list(String keyword,Integer parentId,Pager pager) {
        Page< ${slave.className}DO> page = this.selectPage(
            new Page<>(pager.getCurPage(), pager.getPageSize()),
            new EntityWrapper< ${slave.className}DO>()
                .eq(parentId!=null,"${parentKey}",parentId)
                .andNew(StringUtils.isNotBlank(keyword),
                "username LIKE concat({0},'%') " +
                "or name LIKE concat({0},'%') ",
                keyword));
        return page;
    }

    @Cacheable
    public ${slave.className}DO get${slave.className}(Integer id) {
        ${slave.className}DO ${slave.camelName} = baseMapper.selectById(id);
        return ${slave.camelName};
    }

    @CacheEvict(allEntries = true)
    public ${slave.className}DO save(${slave.className}DO ${slave.camelName}) {
        if (${slave.camelName}.getId() != null) {
            baseMapper.updateById(${slave.camelName});
        }else{
            <#list slave.columnList as item>
                <#if item.name=="createTime">
                ${slave.camelName}.setCreateTime(LocalDateTime.now());
                </#if>
            </#list>
            baseMapper.insert(${slave.camelName});
        }
        return ${slave.camelName};
    }

    @CacheEvict(allEntries = true)
    public void delete(Set<Integer> ids){
        if(!ids.isEmpty()){
            this.baseMapper.deleteBatchIds(ids);
        }
    }

}
